package com.api.controller;


import com.api.annotation.Login;
import com.api.common.constant.Constant;
import com.api.common.page.PageData;
import com.api.common.utils.R;
import com.api.common.utils.Result;
import com.api.entity.Item;
import com.api.entity.ItemInfo;
import com.api.entity.dto.UserDTO;
import com.api.service.ItemInfoService;
import com.api.service.ItemService;
import com.api.service.UploadService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 章节表 前端控制器
 * </p>
 *
 * @author author
 * @since 2024-09-30
 */
@RestController
@RequestMapping("/api/chapter")
@AllArgsConstructor
public class ItemInfoController {
    private final ItemInfoService infoService;
    private  final ItemService iItemService;
    @Autowired
    private UploadService uploadService;

    @Login
    @GetMapping("page")
    @Operation(summary = "分页")
    @Parameters({
            @Parameter(name = Constant.PAGE, description = "当前页码，从1开始", in = ParameterIn.QUERY, required = true, ref = "int"),
            @Parameter(name = Constant.LIMIT, description = "每页显示记录数", in = ParameterIn.QUERY, required = true, ref = "int"),
            @Parameter(name = Constant.ORDER_FIELD, description = "排序字段", in = ParameterIn.QUERY, ref = "String"),
            @Parameter(name = Constant.ORDER, description = "排序方式，可选值(asc、desc)", in = ParameterIn.QUERY, ref = "String"),
            @Parameter(name = "name", description = "章节名称", in = ParameterIn.QUERY, ref = "String"),

    })
    public Result<PageData<ItemInfo>> page(@RequestParam Map<String, Object> params){
        PageData<ItemInfo> page = infoService.page(params);
        for (ItemInfo item : page.getList()) {
            item.setImg(uploadService.toAbsoluteUrl(item.getImg()));
            item.setTitle(iItemService.selectById(item.getItemId()).getTitle());
        }
        return new Result<PageData<ItemInfo>>().ok(page);
    }

    @DeleteMapping("{id}")
    public Result del(@PathVariable Integer id) {
        infoService.deleteById(id);
        return new Result();
    }
    @GetMapping("list")
    public Result list(@RequestParam Integer itemId,@RequestParam(required = false) Integer chapter) {
        LambdaQueryWrapper<ItemInfo> lwq = new LambdaQueryWrapper<>();
       if (chapter != null) {
           lwq.eq(ItemInfo::getChapter, chapter);
       }
        lwq.eq(ItemInfo::getItemId, itemId);
        lwq.orderByAsc(ItemInfo::getSort);
        List<ItemInfo> list = infoService.selectList(lwq);
        if (list.isEmpty()){
            return  new Result().ok(new ArrayList<>());
        }
        for (ItemInfo item : list) {
            item.setImg(uploadService.toAbsoluteUrl(item.getImg()));
        }
        return new Result().ok(list);
    }
    @PostMapping("save")
    public Result save(@RequestBody ItemInfo itemInfo) {
        LambdaQueryWrapper<ItemInfo> lwq = new LambdaQueryWrapper<>();
        lwq.eq(ItemInfo::getItemId,itemInfo.getItemId());
        lwq.eq(ItemInfo::getChapter,itemInfo.getChapter());
        if (infoService.exists(lwq)){
            return new Result().error("章节已经存在，不能重复添加");
        }
        itemInfo.setImg(uploadService.deleteFile(itemInfo.getImg()));
        infoService.insert(itemInfo);
        return new Result();
    }

    @PostMapping("update")
    public Result update(@RequestBody ItemInfo itemInfo) {
        LambdaQueryWrapper<ItemInfo> lwq = new LambdaQueryWrapper<>();
        lwq.eq(ItemInfo::getItemId,itemInfo.getItemId());
        lwq.eq(ItemInfo::getChapter,itemInfo.getChapter());
        if (infoService.exists(lwq)){
            return new Result().error("章节已经存在，不能重复添加");
        }
        itemInfo.setImg(uploadService.deleteFile(itemInfo.getImg()));
        infoService.updateById(itemInfo);
        return new Result();
    }

}
